package com.android.webserver.fwapi;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class RawRessource extends WebPage
{

	static private String		DRAWABLE_CONTENT_TYPE	= "image/jpeg";
	static private String		TEXT_CONTENT_TYPE	= "text/plain";
	static private String		JAVASCRIPT_CONTENT_TYPE	= "text/javascript";
	static private String		CSS_CONTENT_TYPE	= "text/css";
	static final public int	DRAWABLE							    				= 0;
	static final public int	TEXT																		= 1;
	static final public int	JAVASCRIPT													= 2;
	static final public int	CSS													= 3;

	public RawRessource(Context context, Request request)
	{
		super(context, request);

	}

	public boolean getImage(Bitmap bitmap)
	{
		if (bitmap != null)
		{
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
			response_contentByteArray = stream.toByteArray();
			header("Content-Length: " + response_contentByteArray.length);
			header("Content-Type: " + DRAWABLE_CONTENT_TYPE);
			return true;
		}
		else
		{
			Log
					.e("WebServer",
							"DrawableRessource:getImage() cannot get decode image");
			return false;
		}
	}
	
	public boolean getImage(int id)
	{
		return (getImage(BitmapFactory.decodeResource(context.getResources(), id)));
	}
	
	

	public boolean getTxtFile(int id, String contentType)
	{
		InputStream inputStream = context.getResources().openRawResource(id);
		if (inputStream != null)
		{
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

			try
			{
				int i = inputStream.read();
				while (i != -1)
				{
					byteArrayOutputStream.write(i);
					i = inputStream.read();
				}
				inputStream.close();
				response_contentByteArray = byteArrayOutputStream.toByteArray();
				header("Content-Length: " + response_contentByteArray.length);
				header("Content-Type: " + contentType);
				return true;
			}
			catch (IOException e)
			{
				Log.e("WebServer",
						"DrawableRessource:getTxtFile() cannot get decode image");
				return false;
			}
		}
		else
		{
			Log
					.e("WebServer",
							"DrawableRessource:getTxtFile() cannot open ressource");
			return false;
		}

	}

	@Override
	protected boolean create(Object... objects)
	{
		if (objects.length != 2)
		{
			Log
					.e("WebServer",
							"DrawableRessource:create() wrong number of arguments");
			return false;
		}
		int type = (Integer) objects[0];
		int id = (Integer) objects[1];
		switch (type)
		{
		case DRAWABLE:
			return getImage(id);
		case TEXT:
			return getTxtFile(id, TEXT_CONTENT_TYPE);
		case JAVASCRIPT:
			return getTxtFile(id, JAVASCRIPT_CONTENT_TYPE);
		case CSS:
			return getTxtFile(id, CSS_CONTENT_TYPE);
		default:
			Log.e("WebServer", "DrawableRessource:create() wrong type");
			return false;
		}

	}

	@Override
	protected void finish()
	{
	}
}
